package com.struc.td;

import java.io.IOException;
import java.io.OutputStream;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

//this is the lost state that will be actived when the player loses
public class Lost extends State{
	private Texture back;
	private Sprite backToMenu;
	private Texture bg;
	private Sprite background;
	private Main m;
	private SpriteBatch sb;
	private Game game;
	private int enemieskilled;
	private int levellost;
	private BitmapFont font;
	String score;
	String[] splitscore;
	String[] diffrentScore;
	private Preferences pref;
	
	public Lost(Main m) {
		pref = Gdx.app.getPreferences("my-preferences");
		this.m = m;
		bg = new Texture(Gdx.files.internal("data/loststate.png"));
		back = new Texture(Gdx.files.internal("data/highscoremenubut.png"));
		backToMenu = new Sprite(back, 120, 181, 171, 71);
		background = new Sprite(bg, 0, 0, 512, 512);
		sb = new SpriteBatch();
		game = (Game) m.getState(2);
		font = new BitmapFont(Gdx.files.internal("data/towerbone2.fnt"), Gdx.files.internal("data/towerbone2.png"), false);
	}

	public boolean keyDown(int arg0) {
		return false;
	}

	public boolean keyTyped(char arg0) {
		return false;
	}

	public boolean keyUp(int arg0) {
		return false;
	}

	public boolean scrolled(int arg0) {
		return false;
	}

	public boolean touchDown(int x, int y, int arg2, int arg3) {
		y = Gdx.graphics.getHeight()-y;
		if (backToMenu.getBoundingRectangle().contains(x,y)) {
			m.setState(1);
		}
		return false;	
	}

	public boolean touchDragged(int arg0, int arg1, int arg2) {
		return false;
	}

	public boolean touchMoved(int arg0, int arg1) {
		return false;
	}

	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		return false;
	}

	public void update() {
		background.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		backToMenu.setBounds(Gdx.graphics.getWidth()/2-backToMenu.getWidth()/2, Gdx.graphics.getHeight()/8, 96, 40);
	}

	//renders the lost state
	public void render() {
		game.render();
		sb.begin();
		background.draw(sb);
		backToMenu.draw(sb, 0.8f);
		font.draw(sb,"LEVEL  :  " + levellost, 10, Gdx.graphics.getHeight()/3+45);
		font.draw(sb, "YOU   KILLED    " + enemieskilled , 10, Gdx.graphics.getHeight()/3+10);	
		sb.end();
	}

	/* Calculate the score of the game that just ended.
	 * If the score is high enough, submit it to highscore.
	 */
	public void enter() {
		Gdx.input.setInputProcessor(this);
		levellost = game.getLevel();
		enemieskilled = game.getLevelProgress();
		if (pref.getString("One").contains("-") || levellost > levelScore(pref.getString("One")) || (levellost == levelScore(pref.getString("One")) && enemieskilled > progressScore(pref.getString("One")))) {
			pref.putString("Five", pref.getString("Four"));
			pref.putString("Four", pref.getString("Three"));
			pref.putString("Three", pref.getString("Two"));
			pref.putString("Two", pref.getString("One"));
			pref.putString("One","Level: " + levellost + " progress: " + enemieskilled);
		} else if (pref.getString("Two").contains("-") || levellost > levelScore(pref.getString("Two")) || (levellost == levelScore(pref.getString("Two")) && enemieskilled > progressScore(pref.getString("Two")))) {
			pref.putString("Five", pref.getString("Four"));
			pref.putString("Four", pref.getString("Three"));
			pref.putString("Three", pref.getString("Two"));
			pref.putString("Two","Level: " + levellost + " progress: " + enemieskilled);
		} else if (pref.getString("Three").contains("-") || levellost > levelScore(pref.getString("Three")) || (levellost == levelScore(pref.getString("Three"))  && enemieskilled > progressScore(pref.getString("Three")))) {
			pref.putString("Five", pref.getString("Four"));
			pref.putString("Four", pref.getString("Three"));
			pref.putString("Three","Level: " + levellost + " progress: " + enemieskilled);
		} else if (pref.getString("Four").contains("-") || levellost > levelScore(pref.getString("Four")) || (levellost == levelScore(pref.getString("Four"))  && enemieskilled > progressScore(pref.getString("Four")))) {
			pref.putString("Five", pref.getString("Four"));
			pref.putString("Four","Level: " + levellost + " progress: " + enemieskilled);
		} else if (pref.getString("Five").contains("-") || levellost > levelScore(pref.getString("Five")) || (levellost == levelScore(pref.getString("Five"))  && enemieskilled > progressScore(pref.getString("Five")))) {
			pref.putString("Five","Level: " + levellost + " progress: " + enemieskilled);
		}
		pref.flush();
	}
	public int levelScore(String s) {
		return Integer.parseInt(s.split(" ")[1]);
	}
	public int progressScore(String s) {
		return Integer.parseInt(s.split(" ")[3]);
	}
}
